package com.cloudmusic.api.utils;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Random;

/**
 * 功能描述:<br>
 * 〈〉
 *
 * @author wangjin
 * @create 2020-10-09
 * @since 1.0.0
 */
@Component
@ConfigurationProperties("qiniuyunutil")
public class QiniuyunUtil {

    //    @Value("${qiniuUrl}")
    private String qiniuUrl;
    //    @Value("${accessKey}")
    private String accessKey;
    //    @Value("${secretKey}")
    private String secretKey;
    //    @Value("${bucket}")
    private String bucket;

    public String getQiniuUrl() {
        return qiniuUrl;
    }

    public void setQiniuUrl(String qiniuUrl) {
        this.qiniuUrl = qiniuUrl;
    }

    public String getAccessKey() {
        return accessKey;
    }

    public void setAccessKey(String accessKey) {
        this.accessKey = accessKey;
    }

    public String getSecretKey() {
        return secretKey;
    }

    public void setSecretKey(String secretKey) {
        this.secretKey = secretKey;
    }

    public String getBucket() {
        return bucket;
    }

    public void setBucket(String bucket) {
        this.bucket = bucket;
    }

    public String uploadPicture(MultipartFile file) throws IOException {
        Configuration configuration = new Configuration(Region.region0());
        UploadManager uploadManager = new UploadManager(configuration);
        String key = getRandomCharacterAndNumber(10) + ".png";//生成随机文件名
        Auth auth = Auth.create(accessKey, secretKey);
        String uptoken = auth.uploadToken(bucket);
        String responseUrl = "";
        try {
            byte[] localFile = file.getBytes();
            Response response = uploadManager.put(localFile, key, uptoken);
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            responseUrl = responseUrl + qiniuUrl + "/" + putRet.key;
        } catch (QiniuException e) {
            Response r = e.response;
        }
        return responseUrl;
    }

    public String uploadMusic(MultipartFile file) throws IOException {
        Configuration configuration = new Configuration(Region.region0());
        UploadManager uploadManager = new UploadManager(configuration);
        String key = getRandomCharacterAndNumber(10) + ".mp3";//生成随机文件名
        Auth auth = Auth.create(accessKey, secretKey);
        String uptoken = auth.uploadToken(bucket);
        String responseUrl = "";
        try {
            byte[] localFile = file.getBytes();
            Response response = uploadManager.put(localFile, key, uptoken);
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            responseUrl = responseUrl + qiniuUrl + "/" + putRet.key;
        } catch (QiniuException e) {
            Response r = e.response;
        }
        return responseUrl;
    }

    public static String getRandomCharacterAndNumber(int length) {
        String val = "";
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num"; // 输出字母还是数字

            if ("char".equalsIgnoreCase(charOrNum)) // 字符串
            {
                int choice = random.nextInt(2) % 2 == 0 ? 65 : 97; // 取得大写字母还是小写字母
                val += (char) (choice + random.nextInt(26));
                // int choice = 97; // 指定字符串为小写字母
                val += (char) (choice + random.nextInt(26));
            } else if ("num".equalsIgnoreCase(charOrNum)) // 数字
            {
                val += String.valueOf(random.nextInt(10));
            }
        }
        return val;
    }
}
